package com.example.fiction.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.example.fiction.app.MyApplication;
import com.example.fiction.exception.ResponseException;
import com.example.fiction.utils.TransferDataUtils;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * 请求结果回调
 * author: 姚鸿杰
 * time: 2019/4/17 21:42
 **/
public class BaseResult<T> implements Observer<String> {

    private final String TAG = "BaseResult";
    private Disposable mDisposable; //用于切断UI线程更新
    private IResponseListener<T> listener;
    private IResponseListener<T[]> listeners;
    private Class<T> ct;
    private Class<T[]> cts;
    private boolean isArray;

    public void disposable() {
        if (mDisposable != null)
            mDisposable.dispose();
    }

    //todo 待添加等待加载  ------------------------------
    private boolean isShowProgress = false;
    //---------------------------------------------------

    private Context context;

    public BaseResult(Context context) {
        this.context = context;
    }

    public BaseResult(Context context, boolean isShowProgress) {
        this.isShowProgress = isShowProgress;
        this.context = context;
    }

    public void setRequestListener(IResponseListener<T> listener, Class<T> mClass) {
        this.listener = listener;
        this.ct = mClass;
        isArray = false;
    }

    /**
     * 该方法返回集合
     *
     * @param listeners
     * @param mClass
     */
    public void setRequestListListener(IResponseListener<T[]> listeners, Class<T[]> mClass) {
        this.listeners = listeners;
        this.cts = mClass;
        isArray = true;
    }

    @Override
    public void onSubscribe(Disposable d) {
        Log.i(TAG, "onSubscribe");
        mDisposable = d;
    }

    @Override
    public void onNext(String request) {
        Log.i(TAG, "onNext");

        Gson gson = new Gson();
        try {
            if (isArray) {
                T[] results = gson.fromJson(request, cts);
                listeners.onSuccess(results);
            } else {
                T result = gson.fromJson(request, ct);
                listener.onSuccess(result);
            }
        } catch (Exception e) {
            Log.e(TAG, "解析出错");
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {

        //todo 先全部打印
//        e.printStackTrace();
        Log.e(TAG, e.getMessage());

        if (e instanceof SocketTimeoutException) {//请求超时
            //todo 请求超时
        } else if (e instanceof ConnectException) {//网络连接超时
            //todo 网络连接超时
        } else if (e instanceof SSLHandshakeException) {//安全证书异常
            //todo 安全证书异常
        } else if (e instanceof HttpException) {//请求的地址不存在
            //todo 请求的地址不存在
            int code = ((HttpException) e).code();
            if (code == 504) {
            } else if (code == 404) {
            } else if (code == 405) {
                //找不到Method Not Allowed
            }
        } else if (e instanceof UnknownHostException) {//域名解析失败
            //todo 域名解析失败
        } else if (e instanceof JsonParseException ||
                e instanceof JSONException ||
                e instanceof ParseException) {
            //todo 解析出错
        } else if(e instanceof ResponseException){
            //todo 请求数据异常
        }else{
            //todo 其他未知错误
        }

    }

    @Override
    public void onComplete() {
        //调用结束
    }

    public interface IResponseListener<T> {
        void onSuccess(T result);

        void onFail(String errorMsg);
    }
}
